/**
 * Created by qingteng.wang on 2017/1/9.
 */

+function ($) {

    $(function () {
        const $saccordion = $('#kh-service-accordion');
        const $sspanel = $saccordion.find('.kh-service-panel');
        const localHrefStr = window.location.href;
        let defaultIndex;
        let curIndex;


        defaultIndex = getDefaultIndex();

        if(defaultIndex){
            curIndex = defaultIndex;

            toggle(null, curIndex);
            //首次滚动到展开的部分
            animateServiceItem($sspanel.eq(curIndex).offset().top);
        }




        $saccordion.on('click.ac', '.kh-svph-title>a,.kh-svph-more,.kh-svpb-btn', function (e) {
            e.stopPropagation();
            e.preventDefault();
            const $curPanel = $(this).parents('.kh-service-panel');
            toggle($curPanel);

        });

        function getDefaultIndex() {
            let index;
            $sspanel.each((i, panel) => {
                let $panel = $(panel);
                let name = $panel.attr('data-name');
                let nameReg = new RegExp(name);
                if (nameReg.test(localHrefStr)) {
                    index = i;
                    return false;
                }
            });
            return index;
        }


        function animateServiceItem(height) {
            $('html,body').animate({
                scrollTop: height,
            })
        }

        function toggle($selector, index) {
            const $curPanel = ($selector) ? $selector : $sspanel.eq(index);
            const $collapse = $curPanel.find('.kh-svp-collapse');
            const $morebtn = $curPanel.find('.kh-svph-more');
            const $shouqibtn = $curPanel.find('.kh-svpb-btn');
            if ($collapse.is(':hidden')) {
                $morebtn.text('收起')
            } else {
                $morebtn.text('more')
            }

            $collapse.stop().slideToggle(800,'linear');

        }


    });

}(jQuery);